package artibella.media.controls 
{
	import artibella.media.AMedia;
	import artibella.media.audio.AudioMedia;
	import artibella.media.events.MediaEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class AudioController extends AController
	{
		public function AudioController() 
		{
			super();
		}
		
		///////  T R A C K
		
		override public function set media( t:AMedia ):void
		{
			_removeMediaListeners();
			
			super.media = t;
			
			_addMediaListeners();
		}
		
		private function _addMediaListeners():void
		{
			if ( _media == null ) return;
			
			_media.addEventListener( Event.COMPLETE, _onLoadComplete );
			_media.addEventListener( Event.ID3, _onID3Complete );
			
			_media.addEventListener( MediaEvent.PLAY, _onPlay );
			_media.addEventListener( MediaEvent.PAUSE, _onPause );
			_media.addEventListener( MediaEvent.STOP, _onStop );
			_media.addEventListener( MediaEvent.SEEK, _onSeek );
			_media.addEventListener( MediaEvent.VOLUME, _onVolume );
			
			_media.addEventListener( MediaEvent.PLAY_COMPLETE, _onPlayComplete );
		}
		
		private function _removeMediaListeners():void
		{
			if ( _media == null ) return;
			
			_media.removeEventListener( Event.COMPLETE, _onLoadComplete );
			_media.removeEventListener( Event.ID3, _onID3Complete );
			
			_media.removeEventListener( MediaEvent.PLAY, _onPlay );
			_media.removeEventListener( MediaEvent.PAUSE, _onPause );
			_media.removeEventListener( MediaEvent.STOP, _onStop );
			_media.removeEventListener( MediaEvent.SEEK, _onSeek );
			_media.removeEventListener( MediaEvent.VOLUME, _onVolume );
			
			_media.removeEventListener( MediaEvent.PLAY_COMPLETE, _onPlayComplete );
		}
		
		// - - - 
		
		private function _onLoadComplete( e:Event ):void
		{
			if ( _time_tool != null ) _time_tool.duration = _media.duration;
			
			addEventListener( Event.ENTER_FRAME, _onEnterFrame );
		}
		
		private function _onID3Complete( e:Event ):void
		{
			var am:AudioMedia = _media as AudioMedia;
			
			if ( am.infos != null ) 
			{
				trace( "ARTIST : " + am.infos.artist );
				trace( "TITLE :" + am.infos.songName );
				trace( "ALBUM : " + am.infos.album );
				trace( "YEAR : " + am.infos.year );
				//trace( "TRACK # : " + am.infos.track );
				//trace( "GENRE : " + am.infos.genre );
				//trace( "COMMENT : " + am.infos.comment );
			}
		}
		
		private function _onEnterFrame( e:Event ):void
		{
			if ( _media.duration > 0 ) 
			{
				var value:int = Math.ceil( ( _media.position / _media.duration ) * 100 );
				if ( _prog_tool != null ) _prog_tool.progUpdate( value / 100 );
			}
			
			if ( _time_tool != null ) _time_tool.timeUpdate( _media.position );
		}
		
		// - - -
		
		protected function _onPlay( e:MediaEvent ):void
		{
			if ( _bt_play != null )
			{
				_bt_play.removeEventListener( MouseEvent.CLICK, _doPlay );
				_bt_play.addEventListener( MouseEvent.CLICK, _doPause );
			}
			
			if( _media.duration > 0 ) addEventListener( Event.ENTER_FRAME, _onEnterFrame );
			//addEventListener( Event.ENTER_FRAME, _onEnterFrame );
		}
		
		protected function _onPlayComplete( e:MediaEvent ):void
		{
			removeEventListener( Event.ENTER_FRAME, _onEnterFrame );
			
			if ( _time_tool != null ) _time_tool.reset();
			if ( _prog_tool != null ) _prog_tool.progUpdate( 0 );
			
			_player.play();
		}
		
		protected function _onPause( e:MediaEvent ):void
		{
			if ( _bt_play != null )
			{
				_bt_play.removeEventListener( MouseEvent.CLICK, _doPause );
				_bt_play.addEventListener( MouseEvent.CLICK, _doPlay );
			}

			removeEventListener( Event.ENTER_FRAME, _onEnterFrame );
		}
		
		protected function _onSeek( e:MediaEvent ):void
		{
			var p:Number = parseInt( e.value ) / _media.duration

			if ( _prog_tool != null ) _prog_tool.progUpdate( p );
		}
		
		protected function _onStop( e:MediaEvent ):void
		{
			if ( _bt_play != null )
			{
				_bt_play.removeEventListener( MouseEvent.CLICK, _doPause );
				_bt_play.addEventListener( MouseEvent.CLICK, _doPlay );
			}

			if ( _prog_tool != null ) _prog_tool.progUpdate( 0 );
			
			removeEventListener( Event.ENTER_FRAME, _onEnterFrame );
		}
		
		protected function _onVolume( e:MediaEvent ):void
		{
			if ( _volume_tool != null ) _volume_tool.volUpdate( Number( e.value ) );
		}

		// - - -
		
		override public function setVolume( n:Number ):void
		{
			_player.defaultVolume = n;
			
			_media.volume = n;
		}
		
		override public function setPauseState():void
		{
			_doPause( null );
		}
		
		override public function setSeek( p:Number ):void
		{
			var millisec:int = p * _media.duration;
			
			_media.seek( millisec, _paused );
		}
		
		override public function setTime( p:Number ):void
		{
			if ( _time_tool != null ) _time_tool.timeUpdate( p * _media.duration );
		}
		
		// - - -
		
		override public function dispose():void
		{
			removeEventListener( Event.ENTER_FRAME, _onEnterFrame );
			
			_removeMediaListeners();
			
			super.dispose();
		}
	}
}